<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\Admin\KnowledgePosStoreRequest;
use App\Models\Course;
use App\Models\Knowledge;
use App\Models\KnowledgePost;
use Arr;
use Illuminate\Http\JsonResponse;

class KnowledgePostController extends AdminBaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return JsonResponse|object
     */
    public function index()
    {
        $list = KnowledgePost::filter(request()->all())
            ->join('tiku_knowledge', 'tiku_knowledge_post.knowledgeId', '=', 'tiku_knowledge.id')
            ->join('tiku_course', 'tiku_knowledge.courseId', '=', 'tiku_course.id')
            ->selectRaw('tiku_knowledge_post.*, tiku_knowledge.knowledgeName, tiku_course.fullName as courseName')
            ->orderByDesc('tiku_knowledge.id')
            ->paginate(\request('perPage', 10));

        return $this->restful($list);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param KnowledgePosStoreRequest $request
     * @return JsonResponse|object
     */
    public function store(KnowledgePosStoreRequest $request)
    {
        $params = $request->validated();

        $knowledgePost = new KnowledgePost($params);
        $knowledgePost->save();

        return $this->restful($knowledgePost);
    }

    /**
     * Display the specified resource.
     *
     * @param int $id
     * @return JsonResponse|object
     */
    public function show(int $id)
    {

        return $this->restful();
    }

    /**
     * Update the specified resource in storage.
     *
     * @param KnowledgePosStoreRequest $request
     * @param int $id
     * @return JsonResponse|object
     */
    public function update(KnowledgePosStoreRequest $request, int $id)
    {
        $params = $request->validated();

        $result = KnowledgePost::where('id', $id)->update(Arr::except($params, 'id'));

        return $this->restful($result);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     * @return JsonResponse|object
     */
    public function destroy(int $id)
    {
        $result = KnowledgePost::where('id', $id)->delete();
        return $this->restful($result);
    }


    /**
     * 获取二级知识点Options
     * @return JsonResponse|object
     */
    public function getKnowledgeOptions() {
        $courses = Course::selectRaw('id as value, courseName as label, studyName, studyType')
            ->orderByDesc('id')
            ->get()
            ->groupBy('studyName');

        $list = collect($courses)->map(function ($group, $groupName) {
            return [
                'label' => $groupName,
                'value' => $group[0]->studyType,
                'children' => $group->map(function ($course) {
                    return [
                        'label' => $course->label,
                        'value' => $course->value,
                        'children' => Knowledge::select(['id as value', 'knowledgeName as label'])
                            ->where('level', 1)
                            ->where('courseId', $course->value)
                            ->orderBy('id')
                            ->get()->map(function ($k1) {
                                return [
                                    'label' => $k1->label,
                                    'value' => $k1->value,
                                    'children' => Knowledge::select(['id as value', 'knowledgeName as label'])
                                        ->where('level', 2)
                                        ->where('parentId', $k1->value)
                                        ->orderBy('id')
                                        ->get()->toArray()
                                ];
                            })
                    ];
                })->toArray()
            ];
        })->values()->toArray();

        return $this->restful($list);
    }
}
